package LC;

/**
 * https://leetcode.com/problems/reverse-words-in-a-string/description/
 * Given an input string, reverse the string word by word.
 * For example,
 * Given s = "the sky is blue",
 * return "blue is sky the".
 * Update (2015-02-12):
 * For C programmers: Try to solve it in-place in O(1) space.
 * click to show clarification.
 */
public class LC_151_ReverseWordsinaString_String {
    public static void main(String[] args) {
        System.out.println(Solution.reverseWords("the sky is blue..."));
    }

    static class Solution {
        static String reverseWords(String s) {
            if (s == null || s.length() == 0)
                return "";
            String[] arr = s.split(" ");
            StringBuilder sb = new StringBuilder();
            for (int i = arr.length - 1; i >= 0; --i) {
                if (!arr[i].equals("")) {
                    sb.append(arr[i]).append(" ");
                }
            }
            return sb.length() == 0 ? "" : sb.substring(0, sb.length() - 1);
        }
    }

}